﻿function ImagePan(image, bounce, startImage, initSpeed) {
	this.spin = true;
	this.bounce = bounce != null ? bounce : false;
	this.moveSpeed = 0;
	this.imgPosX = 0;
	this.ctrlMove = false;
	this.lastX;
	this.increaseSpeed = 1;
	this.maxSpeed = 20;
	this.delayTime = 25;
	this.panningDisplay = image;
	this.imageInfo = null;
	this.waitingForImage = false;
	var t = this;
	if(!this.bounce)
		$(image).mouseup(function(e) { t.endMove(e); }).mousedown(function(e) { t.startMove(e); }).mousemove(function(e) { t.mouseMove(e); });
	jQuery.data(image, "pan", this);
	if (startImage != null)
		this.setImage(startImage);
	if (initSpeed != null)
		this.moveAt(initSpeed);
}

ImagePan.prototype.panImage = function() {
	var t = this;
	if (t.panningDisplay == null) return;
	if (t.waitingForImage && t.imageInfo.complete) {
		t.waitingForImage = false;
		t.panningDisplay.style.backgroundImage = "url(" + t.imageInfo.src + ")";
	}
	if (t.moveSpeed != 0 || t.waitingForImage) {
		t.panningDisplay.style.backgroundPosition = (t.imgPosX += t.moveSpeed) + "px 0px";
		if (t.bounce == true && !t.waitingForImage) {
			if (t.moveSpeed > 0 && t.imgPosX >= 0)
				t.moveSpeed = -t.moveSpeed;
			else if (Math.abs(t.imgPosX) >= t.imageInfo.width - $(t.panningDisplay).width())
				t.moveSpeed = -t.moveSpeed;
		}
		if (t.spin == true || t.waitingForImage)
			setTimeout(function() { t.panImage(); }, t.delayTime);
	}
}


ImagePan.prototype.stop = function() {
	if (this.panningDisplay == null) return;
	this.moveSpeed = 0;
}

ImagePan.prototype.moveAt = function(speed) {
	var t = this;
	if (t.panningDisplay == null) return;
	var oldMoveSpeed = t.moveSpeed;
	if (t.moveSpeed == 0)
		t.moveSpeed = speed;
	else if (t.moveSpeed < 0)
		t.moveSpeed = -Math.abs(speed);
	else
		t.moveSpeed = Math.abs(speed);

	if (oldMoveSpeed == 0)
		t.panImage();
}

ImagePan.prototype.moveMore = function(dir) {
	var t = this;
	if (t.panningDisplay == null) return;
	var oldMoveSpeed = t.moveSpeed;
	if (dir == "left")
		t.moveSpeed -= t.increaseSpeed;
	else
		t.moveSpeed += t.increaseSpeed;

	if (oldMoveSpeed == 0)
		t.panImage();
}

ImagePan.prototype.startMove = function(e) {
	if (this.panningDisplay == null) return;
	this.ctrlMove = true;
	this.lastX = e.pageX;
}
ImagePan.prototype.endMove = function(e) {
	if (this.panningDisplay == null) return;
	this.ctrlMove = false;
}
ImagePan.prototype.mouseMove = function(e) {
	var t = this;
	if (t.panningDisplay == null) return;
	if (t.ctrlMove) {
		var x = e.pageX; //window.event.clientX;
		if (t.spin) {
			//alert("d");
			var oldMoveSpeed = t.moveSpeed;
			if ((x - t.lastX >= 5) && t.moveSpeed < t.maxSpeed)
				t.moveSpeed += t.increaseSpeed;
			else if ((t.lastX - x >= 5) && t.moveSpeed > -t.maxSpeed)
				t.moveSpeed -= t.increaseSpeed;
			if (oldMoveSpeed == 0)
				t.panImage();
			if (oldMoveSpeed != t.moveSpeed)
				t.lastX = x;
		}
		else
			t.panningDisplay.style.backgroundPositionX = (t.imgPosX += (x - t.lastX));
	}
}
ImagePan.prototype.setImage = function(imgFile) {
	var t = this;
	if (t.panningDisplay == null) return;

	t.imageInfo = new Image();
	t.imageInfo.src = imgFile;
	t.waitingForImage = true;
	if (t.panningDisplay == null) return;

	if (t.moveSpeed == 0)
		t.panImage();
}

function getPan(id) {
	return jQuery.data($("#" + id).get(0), "pan");
}